package com.gwclh.personnel.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.gwclh.personnel.entity.Dismission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import com.gwclh.personnel.entity.Internship;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

/**
 * <p>
 * 员工离职记录表 Mapper 接口
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
public interface DismissionMapper extends BaseMapper<Dismission> {

    @Select("select t1.id,t1.emp_name,t1.dept_id,t2.dept_name from t_employee t1 left join t_department t2 on t1.dept_id=t2.id ${ew.customSqlSegment}")
    IPage<Map<String,Object>> getAll(IPage<Map<String,Object>> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select t1.id,t1.emp_name,t1.dept_id,t2.dept_name from t_employee t1 left join t_department t2 on t1.dept_id=t2.id where t2.dept_name=#{deptName} or t1.id=#{empId} or t1.emp_name=#{empName}")
    IPage<Map<String,Object>> getEmpBySearch(IPage<Map<String,Object>> page, @Param(Constants.WRAPPER) Wrapper wrapper, String deptName, Integer empId, String empName);

    @Select("select t1.id,t1.emp_name,t2.dept_name,t3.sta_name,t4.dis_type\n" +
            "from t_employee t1 \n" +
            "left join t_department t2 \n" +
            "on t1.dept_id=t2.id\n" +
            "left join t_station t3\n" +
            "on t1.sta_id=t3.id\n" +
            "left join t_dismission t4\n" +
            "on t1.id=t4.emp_id")
    IPage<Map<String,Object>> selectQuit(IPage<Map<String,Object>> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select t1.id,t1.emp_name,t2.dept_name,t3.sta_name,t4.dis_type\n" +
            "from t_employee t1 \n" +
            "left join t_department t2 \n" +
            "on t1.dept_id=t2.id\n" +
            "left join t_station t3\n" +
            "on t1.sta_id=t3.id\n" +
            "left join t_dismission t4\n" +
            "on t1.id=t4.emp_id where t1.id=#{empId} or t2.dept_name=#{deptName} or t3.sta_name=#{staName}")
    IPage<Map<String,Object>> selectQuitBySearch(IPage<Map<String,Object>> page, @Param(Constants.WRAPPER) Wrapper wrapper, Integer empId, String deptName, String staName);
    /**
     * 统计月离职人数
     * @return
     */
    @Select("SELECT DATE_FORMAT(dis_date,'%Y-%m') as time,count(*) as total from t_dismission WHERE DATE_FORMAT(dis_date,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') GROUP BY time")
    Map<String,Object> statisticsDismissionEmpMonth();


    @Select("SELECT COUNT(*) as count " +
            "FROM t_dismission di,t_employee e,t_department de " +
            "WHERE di.emp_id = e.id " +
            "AND e.dept_id = de.id " +
            "AND de.id = #{id} " +
            "AND DATE_FORMAT(di.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')")
    Map<String,Object> selectDimissionDataByDeptId(Integer id);
}
